package com.diandian.backend.entrance.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.diandian.backend.entrance.client.DianDianDouyinLifeExchangeClient;
import com.diandian.backend.entrance.client.dto.*;
import com.diandian.backend.entrance.constants.EntranceConstant;
import com.diandian.backend.entrance.dto.ClientPermissionCacheDataDTO;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RouterDouyinLifeService {

    private static final Logger logger = LoggerFactory.getLogger(RouterDouyinLifeService.class);

    @Autowired
    private DianDianDouyinLifeExchangeClient douyinLifeExchangeClient;

    /**
     * 子路由服务
     * @param clientKey
     * @param clientSecret
     * @param data
     * @return
     */
    public Object execute(ClientPermissionCacheDataDTO clientInfoDTO, String clientKey, String clientSecret, String cmd, JSONObject data) {

        // 获取token
        if(EntranceConstant.ROUTER_DOUYIN_LIFE_TOKEN_GET.equals(cmd)){

            logger.info("执行accessToken获取请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTSECRET);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret)){
                throw new DefaultException("配置缺失");
            }

            // 执行请求
            BaseResult resultDTO = douyinLifeExchangeClient.getToken(appKey, appSecret);

            logger.info("执行accessToken获取请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 查询指定日期的抖音订单
        if(EntranceConstant.ROUTER_DOUYIN_LIFE_ORDER_QUERY_TIME.equals(cmd)){

            logger.info("执行查询指定日期的抖音订单请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTSECRET);
            String account = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_ACCOUNT);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret) || account == null || "".equals(account)){
                throw new DefaultException("配置缺失");
            }

            // 执行请求
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            BaseResult resultDTO = douyinLifeExchangeClient.queryByTime(appKey, appSecret, account, data.getString("date"));

            logger.info("行查询指定日期的抖音订单请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 查询指定单号的抖音订单
        if(EntranceConstant.ROUTER_DOUYIN_LIFE_ORDER_QUERY_NUM.equals(cmd)){

            logger.info("执行查询指定单号的抖音订单请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTSECRET);
            String account = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_ACCOUNT);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret) || account == null || "".equals(account)){
                throw new DefaultException("配置缺失");
            }

            // 执行请求
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            BaseResult resultDTO = douyinLifeExchangeClient.queryByNum(appKey, appSecret, account, data.getString("orderNum"));

            logger.info("执行查询指定单号的抖音订单请求: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 积分等级更新
        if(EntranceConstant.ROUTER_DOUYIN_LIFE_MEMBER_POINTS_CHANGE.equals(cmd)){

            logger.info("执行积分等级更新请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTSECRET);
            String account = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_ACCOUNT);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret) || account == null || "".equals(account)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            MemberPointsChangeReqDTO dataDTO = JSONObject.parseObject(data.toString(), MemberPointsChangeReqDTO.class);
            BaseResult resultDTO = douyinLifeExchangeClient.pointsChange(appKey, appSecret, account, dataDTO);

            logger.info("执行积分等级更新请求: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 会员手机号更新
        if(EntranceConstant.ROUTER_DOUYIN_LIFE_MEMBER_MOBILE_CHANGE.equals(cmd)){

            logger.info("执行会员手机号更新请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTSECRET);
            String account = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_ACCOUNT);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret) || account == null || "".equals(account)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            MemberMobileChangeReqDTO dataDTO = JSONObject.parseObject(data.toString(), MemberMobileChangeReqDTO.class);
            BaseResult resultDTO = douyinLifeExchangeClient.mobileChange(appKey, appSecret, account, dataDTO);

            logger.info("执行会员手机号更新请求: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 会员解绑
        if(EntranceConstant.ROUTER_DOUYIN_LIFE_MEMBER_UNBIND.equals(cmd)){

            logger.info("执行会员解绑请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTSECRET);
            String account = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_ACCOUNT);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret) || account == null || "".equals(account)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            MemberUnbindChangeReqDTO dataDTO = JSONObject.parseObject(data.toString(), MemberUnbindChangeReqDTO.class);
            BaseResult resultDTO = douyinLifeExchangeClient.unbind(appKey, appSecret, account, dataDTO);

            logger.info("执行会员解绑请求: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 验券准备
        if(EntranceConstant.ROUTER_DOUYIN_LIFE_CERTIFICATE_VERIFICATION_PREPARE.equals(cmd)){

            logger.info("执行验券准备请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTSECRET);
            String account = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_ACCOUNT);
            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret) || belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            CertificatePrepareReqDTO dataDTO = JSONObject.parseObject(data.toString(), CertificatePrepareReqDTO.class);
            BaseResult resultDTO = douyinLifeExchangeClient.prepare(appKey, appSecret, account, belong, dataDTO);

            logger.info("执行验券准备请求: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 验券
        if(EntranceConstant.ROUTER_DOUYIN_LIFE_CERTIFICATE_VERIFICATION_VERIFY.equals(cmd)){

            logger.info("执行验券请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DOUYIN_LIFE, EntranceConstant.SETTING_TYPE_DOUYIN_LIFE_CLIENTSECRET);
            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret) || belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            CertificateVerifyReqDTO dataDTO = JSONObject.parseObject(data.toString(), CertificateVerifyReqDTO.class);
            BaseResult resultDTO = douyinLifeExchangeClient.verify(appKey, appSecret, belong, dataDTO);

            logger.info("执行验券请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        throw new DefaultException("暂不支持此操作");
    }

}
